package com.lw.controller;

import com.lw.company.Company;
import com.lw.service.CompanyService;
import com.lw.utils.IdWorker;
import com.lw.utils.Result;
import com.lw.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/company")
public class CompanyController{
    @Autowired
    private CompanyService companyService;
    @Autowired
    private IdWorker idWorker;

    /**
     * 更新或保存
     * @param company
     * @return
     */
    @PostMapping("/edit")
    public Result edit(Company company){
        Company temp=new Company();
        if (company.getId()!=null){ //更新
            temp = companyService.getById( company.getId() );
            temp.setName(company.getName());
        }else {
            //保存
            String id=idWorker.nextId()+"";
            temp.setId( id );
            temp.setAuditState("0");//0未审核 1已审核
            temp.setState( 1 ); //0未激活 1/已激活
            temp.setName( "lwwwl" );
            temp.setManagerId( "lwwwl" );
            temp.setVersion( "13" );
            temp.setCreateTime( new Date() );
        }
        companyService.saveOrUpdate( temp );
        return Result.SUCCESS();
    }

    /**
     * 通过id查询企业
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam(value = "id") String id) {
        Company company = companyService.getById( id );
        return new Result( ResultCode.SUCCESS, company );
    }

    /**
     * 通过id删除企业
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam(value = "id") String id){
        companyService.removeById( id );
        return Result.SUCCESS();
    }

    /**
     * 查询全部企业
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Company> companies = companyService.list();
        return new Result( ResultCode.SUCCESS,companies);
    }
}
